package com.freud.logic.authorize.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.freud.framework.base.BaseController;
import com.freud.framework.util.MD5Util;
import com.freud.logic.authorize.beans.User;
import com.freud.logic.authorize.service.AuthorizeService;

@Controller("AuthorizeController")
@RequestMapping("/auth")
public class AuthorizeController extends BaseController {

	/**
	 * The LOGGER.
	 */
	private static final Logger LOGGER = Logger.getLogger(AuthorizeController.class);

	@Autowired
	private AuthorizeService authorizeService;

	@RequestMapping(value = { "/login" }, method = RequestMethod.GET)
	public String login() {
		LOGGER.debug("/auth/login Page visited");
		return "auth.login";
	}

	@RequestMapping(value = { "/register" }, method = RequestMethod.GET)
	public ModelAndView register() {
		LOGGER.debug("/auth/register Page visited");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("auth.register");
		return mav;
	}

	@RequestMapping(value = { "/register" }, method = RequestMethod.POST)
	public String registerPOST(User user) throws Exception {
		user.setPassword(MD5Util.MD5(user.getPassword()).toLowerCase());
		authorizeService.insert(user);
		LOGGER.debug("/auth/register Page visited");
		return "redirect:/index";
	}

	@RequestMapping(value = { "/accessdenied" }, method = RequestMethod.GET)
	public String accessDenied() {
		LOGGER.debug("/auth/access_denied Page visited");
		return "auth.access_denied";
	}
}
